package com.letchat.controller;

import com.letchat.utils.JwtUtils;
import com.letchat.pojo.Result;
import com.letchat.pojo.User;
import com.letchat.service.impl.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
public class RegisterController {

    @Autowired
    RegisterService registerService;

    /**
     * 发送验证码
     *
     * @param email
     * @return
     */
    @GetMapping("/getCaptcha")
    public Result getCaptcha(String email) {
        log.info("邮箱{}", email);
        registerService.getCaptcha(email);
        return Result.success();
    }

    /**
     * 注册
     *
     * @param email
     * @param captcha
     * @return
     */
    @GetMapping("/register")
    public Result register(String email, String captcha) {
        log.info("邮箱{},验证码{}", email, captcha);
        String token = registerService.register(email, captcha);
        if (token != null) return Result.success(token);
        if (registerService.emailIsExist(email)) return Result.error("邮箱已存在");
        return Result.error("验证码过期或错误");
    }
}
